package ads.corporativa.aula3004.tdd;

import java.util.HashMap;
import java.util.Map;

public class ConversorRomanoToDecimal {

	private static Map<Character, Integer> symbols = new HashMap<Character, Integer>() {
		private static final long serialVersionUID = 1L;
		{
			put('I', 1);
			put('V', 5);
			put('X', 10);
			put('L', 50);
			put('C', 100);
			put('D', 500);
			put('M', 1000);
		}
	};

	public static int convert(String numRomano) {
		if (numRomano == null)
			throw new IllegalArgumentException("Numero nao pode ser null");

		Integer result = 0;
		Integer value = null;
		Integer oldValue = null;

		for (int i = 0; i < numRomano.length(); i++) {
			if (symbols.containsKey(numRomano.charAt(i))) {
				value = symbols.get(numRomano.charAt(i));

				if ((oldValue != null && oldValue > 0) && value > oldValue) {
					result += (value * -1);
				} else {
					result += value;
				}

				oldValue = value;
			}
		}

		return Math.abs(result);
	}

}
